package com.happle.gcmclient.config;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class CommonUtilities {
	public static String REGISTRATION_ID = "REG_ID";
	public static String SETTING_SOUND = "SETTING_SOUND";
	public static String SETTING_VIBRATE = "SETTING_VIBRATE";
	public static String SETTING_LANGUAGE = "SETTING_LANGUAGE";
	public static String SETTING_LANGUAGE_CODE = "SETTING_LANGUAGE_CODE";

	private static CommonUtilities instance;
	private Context context;

	private CommonUtilities() {
	}

	/**
	 * Constructor
	 * 
	 * @param context
	 * @return
	 */
	public static CommonUtilities getInstance(Context context) {
		if (instance == null) {
			instance = new CommonUtilities();
			instance.context = context;
		}
		return instance;
	}

	// ============ FUNCTIONS =============== //

	public boolean isRegistrationNull() {
		if (REGISTRATION_ID == null || REGISTRATION_ID.equalsIgnoreCase("")) {
			return true;
		} else {
			return false;
		}
	}

	public String generateGUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	public static SharedPreferences sPref;

	public void settingVibrate(boolean status) {
		putBooleanValue(SETTING_VIBRATE, status);
	}

	public boolean getSettingVibrate() {
		return getBooleanValue(SETTING_VIBRATE);
	}

	public void settingSound(boolean status) {
		putBooleanValue(SETTING_SOUND, status);
	}

	public boolean getSettingSound() {
		return getBooleanValue(SETTING_SOUND);
	}

	public boolean savePreverences(Context context, String prefName,
			String prefValue) {
		boolean result = false;
		sPref = context.getSharedPreferences(Constants.PREFERENCES_FILENAME, 0);
		Editor ed = sPref.edit();
		ed.putString(prefName, prefValue);
		result = ed.commit();
		return result;
	}

	public String pullPreferences(Context context, String prefName) {
		sPref = context.getSharedPreferences(Constants.PREFERENCES_FILENAME, 0);
		String savedText = sPref.getString(prefName, "");
		return savedText;
	}

	public boolean isRegisteredSharedPref(Context context) {
		boolean result = false;
		sPref = context.getSharedPreferences(Constants.PREFERENCES_FILENAME, 0);
		String savedText = sPref.getString(CommonUtilities.REGISTRATION_ID, "");
		if (savedText != "" && savedText != null)
			result = true;
		return result;
	}

	public boolean isOnline(Context context) {
		boolean result = false;
		sPref = context.getSharedPreferences(Constants.PREFERENCES_FILENAME, 0);
		String savedText = sPref.getString(Constants.IS_ONLINE, "");
		if (savedText != "" && savedText != null) {
			if (savedText.equals(Constants.STATUS_ON))
				result = true;
		}
		return result;
	}

	public String generateGUID(Context context) {
		return UUID.randomUUID().toString();
	}

	public boolean checkRunningActivities(Context context, String activityName) {

		boolean res = false;
		ArrayList<String> runningactivities = new ArrayList<String>();
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> services = activityManager
				.getRunningTasks(Integer.MAX_VALUE);

		for (int i1 = 0; i1 < services.size(); i1++) {
			runningactivities.add(0, services.get(i1).topActivity.toString());
		}

		if (runningactivities.contains("ComponentInfo{" + activityName + "}") == true) {
			res = true;
		}
		return res;
	}

	public boolean isActivityRunning(Context context, Class activityClass) {
		ActivityManager activityManager = (ActivityManager) context
				.getApplicationContext().getSystemService(
						Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningTaskInfo> tasks = activityManager
				.getRunningTasks(Integer.MAX_VALUE);

		for (ActivityManager.RunningTaskInfo task : tasks) {
			if (activityClass.getCanonicalName().equalsIgnoreCase(
					task.baseActivity.getClassName()))
				return true;
		}

		return false;
	}

	// ======================== CORE FUNCTIONS ========================

	/**
	 * Save a long integer to SharedPreferences
	 * 
	 * @param key
	 * @param n
	 */
	public void putLongValue(String key, long n) {
		// SmartLogger.log(TAG, "Set long integer value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.PREFERENCES_FILENAME, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putLong(key, n);
		editor.commit();
	}

	/**
	 * Read a long integer to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public long getLongValue(String key) {
		// SmartLogger.log(TAG, "Get long integer value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.PREFERENCES_FILENAME, 0);
		return pref.getLong(key, 0);
	}

	/**
	 * Save an integer to SharedPreferences
	 * 
	 * @param key
	 * @param n
	 */
	public void putIntValue(String key, int n) {
		// SmartLogger.log(TAG, "Set integer value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.PREFERENCES_FILENAME, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(key, n);
		editor.commit();
	}

	/**
	 * Read an integer to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public int getIntValue(String key) {
		// SmartLogger.log(TAG, "Get integer value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.PREFERENCES_FILENAME, 0);
		return pref.getInt(key, 0);
	}

	/**
	 * Save an string to SharedPreferences
	 * 
	 * @param key
	 * @param s
	 */
	public void putStringValue(String key, String s) {
		SharedPreferences pref = context.getSharedPreferences(
				Constants.PREFERENCES_FILENAME, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(key, s);
		editor.commit();
	}

	/**
	 * Read an string to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public String getStringValue(String key) {
		SharedPreferences pref = context.getSharedPreferences(
				Constants.PREFERENCES_FILENAME, 0);
		return pref.getString(key, "");
	}

	/**
	 * Read an string to SharedPreferences
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public String getStringValue(String key, String defaultValue) {
		// SmartLogger.log(TAG, "Get string value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.PREFERENCES_FILENAME, 0);
		return pref.getString(key, defaultValue);
	}

	/**
	 * Save an boolean to SharedPreferences
	 * 
	 * @param key
	 * @param s
	 */
	public void putBooleanValue(String key, boolean b) {
		// SmartLogger.log(TAG, "Set boolean value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.PREFERENCES_FILENAME, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(key, b);
		editor.commit();
	}

	/**
	 * Read an boolean to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public boolean getBooleanValue(String key) {
		// SmartLogger.log(TAG, "Get boolean value");
		try {
			SharedPreferences pref = context.getSharedPreferences(
					Constants.PREFERENCES_FILENAME, 0);
			return pref.getBoolean(key, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			return false;
		}
	}

	/**
	 * Save an float to SharedPreferences
	 * 
	 * @param key
	 * @param s
	 */
	public void putFloatValue(String key, float f) {
		SharedPreferences pref = context.getSharedPreferences(
				Constants.PREFERENCES_FILENAME, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putFloat(key, f);
		editor.commit();
	}

	/**
	 * Read an float to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public float getFloatValue(String key) {
		SharedPreferences pref = context.getSharedPreferences(
				Constants.PREFERENCES_FILENAME, 0);
		return pref.getFloat(key, 0.0f);
	}

}
